# -------------------------------------------------------------------
# Purpose: Creates Figure B15
# Author:  Max Posch, 25/07/2025
# Usage:   Source this script to generate both panels of the figure
# -------------------------------------------------------------------
# Check that required paths exist
stopifnot(dir.exists(pdataconfidential))
stopifnot(dir.exists(pdataconfinterim))
stopifnot(dir.exists(poutputappendix))


## Load patent data
patents <- fread(file.path(pdataconfidential, "patents_fyear_iyear.csv"))


## Add inventors
inventor_names <- fread(file.path(pdataconfinterim, "cusp_patents_inventor_surnames_fips.csv"))
inventor_names <- unique(na.omit(inventor_names))
setkey(inventor_names, patnum)
setkey(patents, patnum)
patents <- merge(inventor_names, patents, all.x = TRUE, allow.cartesian = TRUE)


# Create left panel
temp <- patents[between(iyear, 1850, 1950)] %>%
    .[, .N, by = c("iyear", "patnum")] %>%
    .[, .(
        x_mean = mean(N),
        se = sd(N) / sqrt(.N)
    ), by = c("iyear")]
p <- temp %>% ggplot(aes(x = iyear, y = x_mean)) +
    geom_col() +
    geom_errorbar(aes(ymax = x_mean + se, ymin = x_mean - se)) +
    scale_y_continuous(limits = c(0, 2)) +
    scale_x_continuous(breaks = seq(1850, 1950, 20)) +
    labs(x = "Year", subtitle = "Avg. number of inventors per patent", y = NULL) +
    theme_minimal()
plotname <- file.path(poutputappendix, "figureB15left.pdf")
ggsave(plotname, p, width = 4.6, height = 4.6, units = "in")

cat("Figure saved to:", plotname, "\n")


# Create right panel
temp <- patents[between(iyear, 1850, 1950)] %>%
    .[, .N, by = c("iyear", "patnum")] %>%
    .[, .(
        share = sum(N > 1) / .N * 100
    ), by = c("iyear")]
p <- temp %>% ggplot(aes(x = iyear, y = share)) +
    geom_line() +
    scale_y_continuous(limits = c(0, 100)) +
    scale_x_continuous(breaks = seq(1850, 1950, 20)) +
    labs(x = "Year", subtitle = "Share of patents with 2+ inventors (%)", y = NULL) +
    theme_minimal()
plotname <- file.path(poutputappendix, "figureB15right.pdf")
ggsave(plotname, p, width = 4.6, height = 4.6, units = "in")

cat("Figure saved to:", plotname, "\n")
